<?php
namespace app\index\controller;
use service\services\QuestionService;
use think\Db;
use think\Request;
/**
 * Description of Student
 *
 * @author zhangqiquan
 */
class Student extends Base{
    public function index(){
        $questionService=new QuestionService();
        $topicList=$questionService->getTopicList();
        $this->assign('topList', $topicList);
        $request = Request::instance();
        $topicId = $request->param('topicId');
        $where=1;
        if($topicId){
            $user_id_arr=Db::name('user_exam_detail')->where(['topicId'=>$topicId])->column('user_id','user_id');
            $where=false;
            if($user_id_arr)$where='id in('.implode(',', $user_id_arr).')';
        }
        $user_data=Db::name('user')->where($where)->paginate(60,false, array('query' => input()));
        $err_num=[];
        $topicList_key=array_keys($topicList);
        $this->assign('count_key', count($topicList_key));
        $topicList_key_count_arr=[];
        $etime_arr=[];
        foreach($user_data as $v){
            $err_num[$v['id']]=Db::name('user_exam_detail')->where(['is_right'=>'0','user_id'=>$v['id'],'q_forms'=>'2'])->count();
            //$topicList_key_count_arr[$v['id']]=Db::name('user_exam_step_log')->where('topicId in('. implode(',', $topicList_key).')')->where(['is_end'=>'1','user_id'=>$v['id']])->count();
            $topicList_key_count_arr[$v['id']]=Db::name('user_questions')->where('topicId in('. implode(',', $topicList_key).')')->where(['is_end'=>'1','user_id'=>$v['id']])->count();
            $etime_arr[$v['id']]=Db::name('user_questions')->where('topicId in('. implode(',', $topicList_key).')')->order('id desc')->where(['is_end'=>'1','user_id'=>$v['id']])->find();
        }
        $data=[
            'list'=>$user_data->toarray(),
            'page'=>$user_data->render(),
            'err_num'=>$err_num,
            'key_count'=>$topicList_key_count_arr,
            'etime_arr'=>$etime_arr
        ];
        $this->assign('user_data', $data);
        return $this->fetch();
    }
    public function errQuestion($user_id){
        $q_forms='2';
        $request = Request::instance();
        if($request->param('q_forms'))$q_forms=$request->param('q_forms');
        $data=Db::name('user_exam_detail')->where(['is_right'=>'0','user_id'=>$user_id,'q_forms'=>$q_forms])->paginate(60,false, input());
        $data=[
            'list'=>$data->toarray(),
            'page'=>$data->render(),
        ];
        $user=Db::name('user')->where(['id'=>$user_id])->find();
        $this->assign('user', $user);
        $this->assign('data', $data);
        return $this->fetch();
    }
    public function errQuestionxx($id){
        $data=Db::name('user_exam_detail')->where(['id'=>$id])->find();
        $this->assign('data', $data);
        return $this->fetch();
    }
}
